package cn.cvs.inteceptor;

import cn.cvs.util.Constants;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//<mvc:interceptors></mvc:interceptors>
public class LoginInteceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object loginUser= request.getSession().getAttribute(Constants.USER_SESSION);
        if (loginUser!=null){
            //已经登录，放过请求
            return true;
        }
        //重定向或登录
        //动态工程名
        //${pageContext.request.contextPath}
        String contextPath = request.getContextPath();
        response.sendRedirect(contextPath+"/toLogin");
        return false;
    }
}
